package view;

import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFormattedTextField;

import control.BeanPanel;
import model.Conta;

public class JanelaConta extends javax.swing.JInternalFrame implements BeanPanel{

	private Conta bean;
	private String title;
	private boolean remover = false;
	private boolean consultar = false;
	private boolean editar = false;

	public JanelaConta(String title){
		bean = new Conta();
		this.title = title;
		initComponents();
	}

	public JanelaConta(String title, boolean remover, boolean editar, boolean consultar){
		this.title = title;
		this.remover = remover;
		this.consultar = consultar;
		this.editar = editar;
		initComponents();

		detalhar();
	}

	public void initComponents(){

		java.awt.GridBagConstraints gridBagConstraints;

		panelBotao = new javax.swing.JPanel();
		btnOk = new javax.swing.JButton();
		btnCancelar = new javax.swing.JButton();
		btnBuscar = new javax.swing.JButton("Buscar");
		panelDadosPessoais = new javax.swing.JPanel();
		panelDadosConta = new javax.swing.JPanel();
		lblNomeCliente = new javax.swing.JLabel();
		txtNomeCliente = new javax.swing.JTextField();
		lblNumeroConta = new javax.swing.JLabel();
		txtNumeroConta = new javax.swing.JTextField();
		lblSaldoConta = new javax.swing.JLabel();
		txtSaldoConta = criarCampoMoeda(0);
		lblSenha = new javax.swing.JLabel();
		txtSenha = new javax.swing.JPasswordField();

		panelBusca = new javax.swing.JPanel();

		radioButtonBuscaNome = new javax.swing.JRadioButton();
		radioButtonBuscaNumeroConta = new javax.swing.JRadioButton();

		ButtonGroup bg = new ButtonGroup();

		bg.add(radioButtonBuscaNome);
		bg.add(radioButtonBuscaNumeroConta);

		setLayout(new java.awt.GridBagLayout());

		panelBotaoBusca = new javax.swing.JPanel();
		panelBusca.setLayout(new java.awt.GridBagLayout());

		radioButtonBuscaNome.setText("Busca por Nome");
		radioButtonBuscaNome.setSelected(true);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 5);
		panelBusca.add(radioButtonBuscaNome, gridBagConstraints);		

		radioButtonBuscaNumeroConta.setText("Busca por Conta");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 2);
		panelBusca.add(radioButtonBuscaNumeroConta, gridBagConstraints);

		panelDadosPessoais.setBorder(BorderFactory.createTitledBorder("Cliente"));
		panelDadosPessoais.setLayout(new java.awt.GridBagLayout());

		lblNomeCliente.setText("Nome:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosPessoais.add(lblNomeCliente, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		gridBagConstraints.weightx = 100.0;
		panelDadosPessoais.add(txtNomeCliente, gridBagConstraints);

		lblSenha.setText("Senha:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosPessoais.add(lblSenha, gridBagConstraints);

		txtSenha.setPreferredSize(new Dimension(100, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosPessoais.add(txtSenha, gridBagConstraints);

		if(remover || editar){
			btnBuscar.setPreferredSize(new Dimension(80, 20));
			panelBotaoBusca.add(btnBuscar);

			gridBagConstraints = new java.awt.GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 2;
			gridBagConstraints.gridwidth = 2;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(2, 1, 1, 1);
			gridBagConstraints.weightx = 100.0;
			panelDadosPessoais.add(panelBotaoBusca, gridBagConstraints);
		}

		if(consultar){
			btnBuscar.setPreferredSize(new Dimension(80, 20));
			panelBotaoBusca.add(btnBuscar);

			gridBagConstraints = new java.awt.GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 3;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(2, 1, 1, 1);
			gridBagConstraints.weightx = 100.0;
			getContentPane().add(panelBotaoBusca, gridBagConstraints);
		}

		panelDadosConta.setBorder(BorderFactory.createTitledBorder("Conta"));
		panelDadosConta.setLayout(new java.awt.GridBagLayout());

		lblNumeroConta.setText("Numero da Conta:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosConta.add(lblNumeroConta, gridBagConstraints);

		txtNumeroConta.setPreferredSize(new Dimension(100, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosConta.add(txtNumeroConta, gridBagConstraints);

		lblSaldoConta.setText("Saldo:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosConta.add(lblSaldoConta, gridBagConstraints);

		txtSaldoConta.setPreferredSize(new Dimension(100, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosConta.add(txtSaldoConta, gridBagConstraints);

		if(consultar){
			gridBagConstraints = new java.awt.GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
			gridBagConstraints.weightx = 100.0;
			getContentPane().add(panelBusca, gridBagConstraints);
		}

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.weighty = 100.0;
		getContentPane().add(panelDadosPessoais, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.weighty = 100.0;
		getContentPane().add(panelDadosConta, gridBagConstraints);

		btnOk.setText("Ok");
		btnCancelar.setText("Cancelar");

		panelBotao.add(btnOk);
		panelBotao.add(btnCancelar);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		getContentPane().add(panelBotao, gridBagConstraints);

		setTitle(title);
		try {
			setIcon(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setVisible(true);
		setClosable(true);
		setResizable(true);
		setSize(new Dimension(390, 300));
		setLocation(200, 89);
	}

	private javax.swing.JPanel panelBotao;
	private javax.swing.JButton btnOk;
	private javax.swing.JButton btnCancelar;
	private javax.swing.JButton btnBuscar;
	private javax.swing.JPanel panelDadosPessoais;
	private javax.swing.JPanel panelDadosConta;
	private javax.swing.JLabel lblNomeCliente;
	private javax.swing.JTextField txtNomeCliente;
	private javax.swing.JLabel lblNumeroConta;
	private javax.swing.JTextField txtNumeroConta;
	private javax.swing.JLabel lblSaldoConta;
	private javax.swing.JFormattedTextField txtSaldoConta;
	private javax.swing.JLabel lblSenha;
	private javax.swing.JPasswordField txtSenha;
	private javax.swing.JPanel panelBusca;
	private javax.swing.JPanel panelBotaoBusca;
	private javax.swing.JRadioButton radioButtonBuscaNome;
	private javax.swing.JRadioButton radioButtonBuscaNumeroConta;

	public void detalhar(){
		if(remover || editar || radioButtonBuscaNome.isSelected()){
			txtNomeCliente.setEnabled(true);
			txtSenha.setEnabled(true);	
			txtNumeroConta.setEnabled(false);
			txtSaldoConta.setEnabled(false);

			if(consultar){
				btnCancelar.setVisible(false);
			}
			return;
		}

		if(!radioButtonBuscaNome.isSelected()){
			txtNomeCliente.setEnabled(false);
			txtSenha.setEnabled(false);
			txtNumeroConta.setEnabled(true);
			txtSaldoConta.setEnabled(false);
			return;
		}	
	}

	public void desdetalhar(){
		txtNomeCliente.setEnabled(true);
		txtSenha.setEnabled(true);
		txtNumeroConta.setEnabled(true);
		txtSaldoConta.setEnabled(true);
	}

	public Object getBean() {

		if(this.bean == null){
			this.bean = new Conta();
		}

		if(txtNomeCliente.getText() != null)
			this.bean.setNomeCliente(txtNomeCliente.getText());
		if(txtSenha.getText() != null)
			this.bean.setSenha(txtSenha.getText());

		if(txtNumeroConta.getText() != null)
			this.bean.setNroConta(Long.parseLong(txtNumeroConta.getText()));
		if(txtSaldoConta.getText() != null)
			this.bean.setSaldo(Float.parseFloat(txtSaldoConta.getText()));

		return bean;
	}

	public void setBean(Object bean) {

		this.bean = (Conta)bean;
		if(this.bean != null){
			txtNomeCliente.setText(this.bean.getNomeCliente());
			txtSenha.setText(this.bean.getSenha());
			txtNumeroConta.setText(this.bean.getNroConta().toString());
			txtSaldoConta.setValue(this.bean.getSaldo());
		}
	}

	public static JFormattedTextField criarCampoMoeda(float valorVazio){  
		NumberFormat nf = new DecimalFormat("#,###.00");  
		JFormattedTextField campo = new JFormattedTextField(nf);  
		campo.setValue(valorVazio);
		return campo;  
	}  

	public javax.swing.JButton getBtnOk() {
		return btnOk;
	}

	public javax.swing.JButton getBtnCancelar() {
		return btnCancelar;
	}	

	public javax.swing.JTextField getTxtNomeCliente() {
		return txtNomeCliente;
	}

	public javax.swing.JTextField getTxtNumeroConta() {
		return txtNumeroConta;
	}

	public javax.swing.JPasswordField getTxtSenha() {
		return txtSenha;
	}

	public javax.swing.JRadioButton getRadioButtonBuscaNome() {
		return radioButtonBuscaNome;
	}

	public javax.swing.JRadioButton getRadioButtonBuscaNumeroConta() {
		return radioButtonBuscaNumeroConta;
	}

	public javax.swing.JButton getBtnBuscar() {
		return btnBuscar;
	}

	public javax.swing.JFormattedTextField getTxtSaldoConta() {
		return txtSaldoConta;
	}
}
